package com.doform.android.xianggucheng.utils;

import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.text.TextUtils;

import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SynthesizerListener;

/**
 * 文字转语音朗读
 * Created by F_Ms-Local on 2016/12/22.
 */
public class Text2SpeechUtils {

    public enum VOICE_TYPE {
        XIAOYAN,
        XIAOYU,
        XIAOQI,
        XIAOYAN2,
        XIAOMEI,
        XIAOXIN
    }

    private Context mContext;
    
    private final AudioManager mAudioManager;
    
    private int mStreamMaxVolume;
    
    private int mOldVolume;

    private VOICE_TYPE mVoiceType = VOICE_TYPE.XIAOYAN;
    
    private final SpeechSynthesizer mTts;

    private String mVoiceSpeed = "50";

    private String mVoiceVolume = "100";

    public Text2SpeechUtils(Context context) { //?? 啥？
        if (context == null) {
            throw new NullPointerException("Context == null");
        }
        mContext = context;

        // AudioManager
        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        mStreamMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

        // 讯飞
        mTts = SpeechSynthesizer.createSynthesizer(context, null);
    }

    public void text2Speech(String text) throws IllegalArgumentException { // debug
        if (TextUtils.isEmpty(text)) {
            throw new IllegalArgumentException("指定被朗读的Text为空");
        }

        /*
        讯飞朗读
         */
        mTts.setParameter(SpeechConstant.VOICE_NAME, getVoiceName(mVoiceType)); // 发音人
        mTts.setParameter(SpeechConstant.SPEED, mVoiceSpeed); // 语速
        mTts.setParameter(SpeechConstant.VOLUME, mVoiceVolume); // 音量
        mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); // 语音合成方式

        /*
        获取当前音量并设置音量为最大
         */
        mOldVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mStreamMaxVolume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);

        mTts.startSpeaking(text, mSyncListener);
    }

    /**
     * Enum -> voicTypeString
     *
     * @param voiceType
     * @return
     */
    private static String getVoiceName(VOICE_TYPE voiceType) {

        switch (voiceType) {
            case XIAOQI:
                return "xiaoqi";
            case XIAOXIN:
                return "xiaoxin";
            case XIAOYAN:
                return "xiaoyan";
            case XIAOYAN2:
                return "vixy";
            case XIAOYU:
                return "xiaoyu";
            case XIAOMEI:
            default:
                return "xiaomei";
        }
    }

    private SynthesizerListener mSyncListener = new SynthesizerListener() {
        @Override
        public void onSpeakBegin() {
        }

        @Override
        public void onBufferProgress(int i, int i1, int i2, String s) {
        }

        @Override
        public void onSpeakPaused() {
        }

        @Override
        public void onSpeakResumed() {
        }

        @Override
        public void onSpeakProgress(int i, int i1, int i2) {
        }

        @Override
        public void onCompleted(SpeechError speechError) {
            if (speechError == null) {
                speechError.printStackTrace();
            }

            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mOldVolume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
        }

        @Override
        public void onEvent(int i, int i1, int i2, Bundle bundle) {
        }
    };

}
